package com.asura.qqeffect.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.AbsListView;
import android.widget.ListView;

/**
 * Created by Asura on 2016/12/8.
 */

public class DemoList extends ListView {
    private boolean isEnd;

    public DemoList(Context context) {
        super(context);
        init();
    }

    public DemoList(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DemoList(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_MOVE:
//                //如果滑到底部了，listview不消费滑动事件
//                if (isEnd) {
//                    getParent().getParent().requestDisallowInterceptTouchEvent(false);
//                    return false;
//                } else {
//                    getParent().getParent().requestDisallowInterceptTouchEvent(true);
//                }
//                break;
//            case MotionEvent.ACTION_UP:
//            case MotionEvent.ACTION_CANCEL:
//                getParent().getParent().requestDisallowInterceptTouchEvent(false);
//                break;
//        }
        return super.dispatchTouchEvent(event);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        getLayoutParams().height = getContext().getResources().getDisplayMetrics().heightPixels;
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private void init() {
        setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                isEnd = firstVisibleItem + visibleItemCount == totalItemCount;
            }
        });
    }

}
